import django_filters
from django.shortcuts import render
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import viewsets
from rest_framework.filters import SearchFilter
from rest_framework.pagination import PageNumberPagination

from user.models import Author, auto
from user.ser import autoserializers, authorserializers


# Create your views here.


class Page(PageNumberPagination):
    page_size = 2
    page_size_query_param = 'size'
    page_query_param = 'page'


class AuthorView(django_filters.FilterSet):
    id=django_filters.NumberFilter(field_name='id',lookup_expr='exact')
    name=django_filters.CharFilter(field_name='name',lookup_expr='icontains')
    username=django_filters.CharFilter(field_name='username',lookup_expr='icontains')
    phone=django_filters.CharFilter(field_name='phone',lookup_expr='icontains')
    stime=django_filters.DateTimeFilter(field_name='stime',lookup_expr='exact')
    class Meta:
        model=Author
        fields=['status','type_id']


class autoser(viewsets.ModelViewSet):
    queryset = auto.objects.all()
    serializer_class =autoserializers


class Authorser(viewsets.ModelViewSet):
    queryset = Author.objects.all()
    serializer_class =authorserializers
    pagination_class = Page
    filter_backends = [DjangoFilterBackend,SearchFilter]
    filterset_class = AuthorView


